package com.aron;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * @version v1.0
 * @ProjectName: studycoding
 * @ClassName: Missile
 * @Description: TODO(坦克导弹实体)
 * @Author: Leo
 * @Date: 2020/6/13 15:22
 */
public class Missile {

    //执行速度
    private static final int SPEED = 50;
    private static Logger logger = LoggerFactory.getLogger(Missile.class);
    //导弹的X，Y轴
    private int x, y;
    //导弹的方向
    private Dir dir;
    //导弹的类型（敌人，友军）
    private TankGroup group;
    //移动方向
    private boolean isL, isU, isR, isD;
    //是否活着
    private boolean isLive = true;
    public Missile(int x, int y, Dir dir, TankGroup group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
    }

    public TankGroup getGroup() {
        return group;
    }

    public void setGroup(TankGroup group) {
        this.group = group;
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public void print(Graphics g) {
        if (this.group == TankGroup.PLAYER) {
            g.drawImage(ResourseMgr.tankMissile, x, y, null);
        }
        if (this.group == TankGroup.ENEMY) {
            g.drawImage(ResourseMgr.enemyMissile, x, y, null);
        }
        move();
    }

    /**
     * 设置坦克在键盘监听到方向键按下的处理
     *
     * @param e
     */
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                isL = true;
                break;
            case KeyEvent.VK_UP:
                isU = true;
                break;
            case KeyEvent.VK_RIGHT:
                isR = true;
                break;
            case KeyEvent.VK_DOWN:
                isD = true;
                break;
        }
        setMainDir();
    }

    /**
     * 设置坦克在键盘监听到方向键放开的处理
     *
     * @param e
     */
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                isL = false;
                break;
            case KeyEvent.VK_UP:
                isU = false;
                break;
            case KeyEvent.VK_RIGHT:
                isR = false;
                break;
            case KeyEvent.VK_DOWN:
                isD = false;
                break;
        }
        setMainDir();
    }

    //设置移动的方向
    private void setMainDir() {
        if (isL && !isU && !isR && !isD)
            dir = Dir.LEFT;
        if (!isL && isU && !isR && !isD)
            dir = Dir.UP;
        if (!isL && !isU && isR && !isD)
            dir = Dir.RIGHT;
        if (!isL && !isU && !isR && isD)
            dir = Dir.DOWN;
    }

    //设置方向的移动
    private void move() {
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        borderLineChk();
    }

    //设置导弹是否存活
    private void borderLineChk() {
        if (x < 0 || y < 30 || x > TankFrame.GAME_W || y > TankFrame.GAME_H) {
            setLive(false);
        }
    }

    /**
     * 设置导弹死亡状态,设置导弹属性为死亡属性
     */
    public void die() {
        this.setLive(false);
    }

    /**
     * 导弹打中坦克处理方法
     *
     * @param tank 坦克
     */
    public void blastWithTank(EnemyTank tank) {
        //如果tank已经死亡则不计算打中爆炸
        if (!tank.isLive()) return;
        Rectangle rectMissile = new Rectangle(x, y, ResourseMgr.tankMissile.getWidth(), ResourseMgr.tankMissile.getHeight());
        Rectangle rectTank = new Rectangle(tank.getX(), tank.getY(), tank.getWidth(), tank.getHeight());
        //判断2个方块重合则定义为导弹打中坦克
        if (rectMissile.intersects(rectTank)) {
            this.die();
            tank.die();
        }
    }
    /**
     * 导弹打中坦克处理方法
     *
     * @param tank 坦克
     */
    public void blastWithTank(PlayerTank tank) {
        //如果tank已经死亡则不计算打中爆炸
        if (!tank.isLive()) return;
        Rectangle rectMissile = new Rectangle(x, y, ResourseMgr.enemyMissile.getWidth(), ResourseMgr.enemyMissile.getHeight());
        Rectangle rectTank = new Rectangle(tank.getX(), tank.getY(), tank.getWidth(), tank.getHeight());
        //判断2个方块重合则定义为导弹打中坦克
        if (rectMissile.intersects(rectTank)) {
            this.die();
            tank.die();
        }
    }
}
